#!/bin/sh
# $Id:$
#
# Bundle up zip file. (also a tgz and a tar as HDX platforn only has tar)
# appinfo.json is include for compatibility with CSI installer.
set -e
#set -x
if [ $# -le 1 ] ; then
    echo "usage: $0 appname version [filelist]"
    exit 1
fi

# $1 = name , rest = files
create_packages() {
    mkdir -p packages
    name="$1" ; shift

    #Create the zip package for direct download by the csi installer.
    zip -x*/.svn/* -x*/*~ -r packages/$name.zip  "$@" 

    #Create the csi inner tar file
    tar --format=oldgnu --exclude=.svn --exclude=*~ -c -f packages/$name.tar "$@"

    #Create the csi outer zip
    cd packages
    zip csi_$name.zip $name.tar 
    rm $name.tar

    cd ..
}

checkpoint() {
    echo ; echo "$@ - y/n" ; read x
    [ "$x" = "y" ]
}

##############################################################################

appname=$1 ; shift

version_id=$1 ; shift

case "$version_id" in
    *r[0-9][0-9]*)
		echo Version $version_id
		;;
	*)
		echo "Invalid version [$version_id]" ;
		exit 1
		;;
esac

rm -f $appname.zip $appname.version $appname.beta.version

#Get version from first file

version_filename=$appname.version

echo $version_id > $version_filename

echo "version_id=$version_id" 

app_id=$appname-$version_id

sed 's/version=.*/version="'"$version_id"'";/' csi/$appname-appinfo.json > appinfo.json

dest=$appname

echo === $version_id ===

grep ^VERSION= "$@" | sed 's/VERSION=//;s/:/	:	/'
echo
echo === latest common files ===
grep ^VERSION= ../nmt-scripts/*.sh
echo

if checkpoint "Create packages for `cat $version_filename` $app_id in ./packages/" ; then

    create_packages "$app_id" appinfo.json $version_filename "$@"

    rm appinfo.json

    rm -f $version_filename
fi 
